package DynamicProgramming;

public class 最后一块石头的重量2 {
    public int lastStoneWeightII(int[] stones) {
        int sum = 0;
        for (int stone : stones) sum += stone;
        int[] dp = new int[sum / 2 + 1];
        for (int stone : stones) {
            for (int i = dp.length - 1; i >= stone; i--) {
                dp[i] = Math.max(dp[i], dp[i - stone] + stone);
            }
        }
        return sum - dp[sum / 2] * 2;
    }
}
